package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.RecordBean;
import com.project.bean.RoomBean;
import com.project.mapper.IClassMapper;
import com.project.mapper.IRecordMapper;
import com.project.mapper.IRoomMapper;
import com.project.service.IRecordService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
@Service
@Transactional
public class RecordServiceImp implements IRecordService {
    @Autowired
    private IRecordMapper recMapper;
    @Autowired
    private IRoomMapper roomMapper;
    @Override
    public ArrayList<RecordBean> showRecords(Integer roomId) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("fk_roomid",roomId);
       return (ArrayList<RecordBean>) recMapper.selectList(qw);
    }

    @Override
    public void addRecord(RecordBean recordBean) {
        recMapper.insert(recordBean);
        RoomBean roomBean = roomMapper.selectById(recordBean.getRoomId());
        roomBean.setRoomState("损坏");
        roomMapper.updateById(roomBean);
    }

    @Override
    public void processRecord(Integer recordId, Integer roomId) {
        RecordBean recordBean = recMapper.selectById(recordId);
        recordBean.setRecordState("已解决");
        recMapper.updateById(recordBean);
        QueryWrapper<RecordBean> qw =new QueryWrapper<>();
        qw.eq("r_state","未解决");
        int num =recMapper.selectCount(qw);
        System.out.println("-------------"+num);
        if(num == 0){
            RoomBean roomBean = roomMapper.selectById(roomId);
            roomBean.setRoomState("正常");
            roomMapper.updateById(roomBean);
        }
    }
}
